import { Button, Form, Input, TextArea, Toast } from 'antd-mobile'
import './AddAddress.scss'
import { addAddressAPI, type addressParmas } from '../../../../api/address'
import { useNavigate } from 'react-router-dom'

type E = {
    name: string,
    address: string,
    mobile: string
}
const AddAddress = () => {
    const navigate = useNavigate()
    const submit = (e: E) => {
        console.log(e)
        const address = e
        addAddress(address)
    }
    async function addAddress(params: addressParmas) {
        try {
            const res = await addAddressAPI(params)
            console.log(res)
            if (res.data.code === 200) {
                if (res.data.data.code === 0) {
                    Toast.show({ content: '添加成功' })
                    navigate(-1)
                }
            }else if (res.data.code===500){
                Toast.show({content:'登录失效请重新登录'})
            }

        } catch (error) {
            console.log(error)
        }

    }



    return <div className='addaddress'>

        <Form className='addressFrom'
            layout='horizontal'
            onFinish={(e) => submit(e)}
            footer={
                <Button block type='submit' color='primary' size='large' >
                    提交
                </Button>
            }
        >
            <Form.Header>地址填写</Form.Header>
            <Form.Item
                name='name'
                label='姓名'
                rules={[{ required: true, message: '姓名不能为空' }]}
            >
                <Input onChange={console.log} placeholder='请输入姓名' />
            </Form.Item>
            <Form.Item
                name='mobile'
                label='联系电话'
                rules={[{ required: true, message: '联系电话不能为空' }]}
            >
                <Input onChange={console.log} placeholder='请输入联系电话' />
            </Form.Item>
            <Form.Item name='address' label='收货地址'
                rules={[{ required: true, message: '地址不能为空' }]}>
                <TextArea
                    placeholder='请输入地址'
                    maxLength={100}
                    rows={2}
                    showCount
                />
            </Form.Item>
        </Form>
    </div>
}

export { AddAddress }